package org.metasketch.editor.toolbox.advanced;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.metasketch.model.CommonIdentified;

public class ScriptRegistry {

	Map<Class, String> scriptPerClass = new HashMap<Class, String>();

	Map<Class, String> inheritedScriptPerClass = new HashMap<Class, String>();

	public void register(Class type, String toolHandler) {
		scriptPerClass.put(type, toolHandler);
	}

	public String get(Class type) throws Throwable {
		return scriptPerClass.get(type);
	}

	public String get(CommonIdentified semantic) throws Throwable {
		return get(semantic.getClass());
	}

	public void registerInherited(Class type, String toolHandler) {
		inheritedScriptPerClass.put(type, toolHandler);
	}

	public String getInherited(Class type) throws Throwable {
		Set<Class> keys = inheritedScriptPerClass.keySet();
		for (Class class1 : keys) {
			if (class1.isAssignableFrom(type))
				return inheritedScriptPerClass.get(class1);
		}

		return null;
	}

	public String getInherited(CommonIdentified semantic) throws Throwable {
		return getInherited(semantic.getClass());
	}

}
